package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.DeptDao;
import com.itheima.domain.system.Dept;
import com.itheima.factory.MapperFactory;
import com.itheima.service.system.DeptService;
import com.itheima.utils.ArrayToListUtil;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;


public class DeptServiceImpl implements DeptService {
    @Override
    public PageInfo findAll(Integer page, Integer size) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        DeptDao mapper = MapperFactory.getMapper(sqlSession, DeptDao.class);
        PageHelper.startPage(page,size);
        List<Dept> all = mapper.findAll();
        PageInfo<Dept> deptPageInfo = new PageInfo<>(all);
        TransactionUtil.close(sqlSession);
        return deptPageInfo;
    }

    @Override
    public List<Dept> findAll() {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        DeptDao mapper = MapperFactory.getMapper(sqlSession, DeptDao.class);
        List<Dept> all = mapper.findAll();
        TransactionUtil.close(sqlSession);
        return all;
    }

    @Override
    public Dept findById(String id) {

        SqlSession sqlSession = MapperFactory.getSqlSession();
        DeptDao mapper = MapperFactory.getMapper(sqlSession, DeptDao.class);
        Dept dept= mapper.findById(id);
        TransactionUtil.close(sqlSession);
        return dept;
    }

    @Override
    public void delete(String...id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            DeptDao mapper = MapperFactory.getMapper(sqlSession, DeptDao.class);
            mapper.delete(ArrayToListUtil.toList(id));
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public void save(Dept dept) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            DeptDao mapper = MapperFactory.getMapper(sqlSession, DeptDao.class);
            dept.setId(UUID.randomUUID().toString());
            dept.setState(1);
            mapper.save(dept);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public void update(Dept dept) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            DeptDao mapper = MapperFactory.getMapper(sqlSession, DeptDao.class);
            mapper.update(dept);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

}
